/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jaspec;

import static junit.framework.Assert.*;

/**
 * Jaspec string handler.
 */
public class JaspecString extends JaspecObject<String> {

  public JaspecString(String obj) {
    super(obj);
  }

  public JaspecString(String obj, boolean not) {
    super(obj, not);
  }

  /**
   * Check the string have a number of...
   * 
   * @param n Number
   */
  public JaspecStringHave have(int n) {
    return new JaspecStringHave(this, n, Have.EXACTLY);
  }

  /**
   * Check the string have at most a number of...
   * 
   * @param n Number
   */
  public JaspecStringHave have_at_most(int n) {
    return new JaspecStringHave(this, n, Have.AT_MOST);
  }

  /**
   * Check the string have at least a number of...
   * 
   * @param n Number
   */
  public JaspecStringHave have_at_least(int n) {
    return new JaspecStringHave(this, n, Have.AT_LEAST);
  }

  /**
   * Check the string include certain substrings.
   * 
   * @param substrings Substrings
   */
  public void include(String... substrings) {
    if (!not) {
      for (String s : substrings) {
        assertTrue("The string should include the substring '" + s + "'", obj.contains(s));
      }
    } else {
      for (String s : substrings) {
        assertFalse("The string should not include the substring '" + s + "'", obj.contains(s));
      }
    }
  }

  /**
   * Check the string matches with regex expressions.
   * 
   * @param regexs Regex expressions
   */
  public void match(String... regexs) {
    if (!not) {
      for (String s : regexs) {
        assertTrue("The string should matches the substring '" + s + "'", obj.matches(s));
      }
    } else {
      for (String s : regexs) {
        assertFalse("The string should not matches the substring '" + s + "'", obj.matches(s));
      }
    }
  }

  /**
   * Check the string starts with a prefix.
   * 
   * @param prefix The prefix
   */
  public void start_with(String prefix) {
    boolean startsWith = obj.startsWith(prefix);
    if (!not) {
      assertTrue("The string should starts with '" + prefix + "'", startsWith);
    } else {
      assertFalse("The string should not starts with '" + prefix + "'", startsWith);
    }
  }

  /**
   * Check the string ends with a suffix.
   * 
   * @param suffix The suffix
   */
  public void end_with(String suffix) {
    boolean endsWith = obj.endsWith(suffix);
    if (!not) {
      assertTrue("The string should end with '" + suffix + "'", endsWith);
    } else {
      assertFalse("The string should not end with '" + suffix + "'", endsWith);
    }
  }
}
